<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once("conn.php");

$image_file = $_FILES ['imageURL'] ['name'];
if ($image_file != "") {
    $path_parts = pathinfo($image_file);
    $filename = "upload/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['imageURL'] ['tmp_name'], $filename);
    $imageURL = $filename;
}

$sound_file = $_FILES ['soundURL'] ['name'];
if ($sound_file != "") {
    $path_parts = pathinfo($sound_file);
    $filename = "upload/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['soundURL'] ['tmp_name'], $filename);
    $soundURL = $filename;
}

$ImageAid1_file = $_FILES ['soundURL'] ['name'];
if ($ImageAid1_file != "") {
    $path_parts = pathinfo($ImageAid1_file);
    $filename = "upload/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['soundURL'] ['tmp_name'], $filename);
    $ImageAid1URL = $filename;
}

$ImageAid2_file = $_FILES ['soundURL'] ['name'];
if ($ImageAid2_file != "") {
    $path_parts = pathinfo($ImageAid2_file);
    $filename = "upload/" . date("YmdHis") . "." . $path_parts ['extension'];
    move_uploaded_file($_FILES ['soundURL'] ['tmp_name'], $filename);
    $ImageAid2URL = $filename;
}

$packid = checkTrim($_POST['packid']);
$packcode = checkTrim($_POST['packcode']);
$name = checkTrim($_POST['name']);
$year = checkTrim($_POST['year']);
$month = checkTrim($_POST['month']);
$age = checkTrim($_POST['age']);

$db = new DBC();

$is_code_exists = false;

if ($packid == "") {
    $sql = "SELECT * FROM t_pack WHERE packcode = '" . $packcode . "'";
//echo $sql;	
    $data = $db->fetchData($sql);
    if ($data != null && count($data) > 0) {
        $is_code_exists = true;
    }
    $sql = "INSERT INTO t_pack(packcode,name,soundURL,imageURL,year,month,age,ImageAid1URL,ImageAid2URL,createTime) VALUES('" . $packcode . "','" . $name . "','" . $soundURL . "','" . $imageURL . "'," . $year . "," . $month . "," . $age . ",'" . $ImageAid1URL . "','" . $ImageAid2URL . "',now())";
} else {
    if ($image_file == "") {
        $image_sql = "";
    } else {
        $image_sql = "imageURL='" . $imageURL . "',";
    }
    if ($sound_file == "") {
        $sound_sql = "";
    } else {
        $sound_sql = "soundURL='" . $soundURL . "',";
    }
    if ($ImageAid1_file == "") {
        $aid1_sql = "";
    } else {
        $aid1_sql = "ImageAid1URL='" . $ImageAid1URL . "',";
    }
    if ($ImageAid2_file == "") {
        $aid2_sql = "";
    } else {
        $aid2_sql = "ImageAid1URL='" . $ImageAid2URL . "',";
    }
    $sql = "UPDATE  t_pack SET packcode = '" . $packcode . "',name='" . $name . "'," . $sound_sql . $image_sql . $aid1_sql . $aid2_sql . "year = " . $year . ", month = " . $month . ", age = " . $age . " WHERE packid = " . $packid;
}
//echo $sql;
if ($is_code_exists) {
    $msg = "编号 " . $packcode . " 已经存在。";
    echo "<script language='javascript'>alert('" . $msg . "'); </script>";
    echo "<script language='javascript'>history.back();</script>";
} else {
    $transfer = "packList.php?year = " . $year . "&month = " . $month . "&age = " . $age;
    if ($db->execute($sql)) {
        //success
        //    echo "<script language = 'javascript'>alert('成功');</ script>";
        echo "<script language='javascript'>location='" . $transfer . "';</script>";
    } else {
        //failue
        echo "<script language = 'javascript'>alert('失败')</script>";
        echo "<script language='javascript'>history.back();</script>";
    }
}

function checkTrim($item) {
    if (isset($item)) {
        $item = trim($item);
    } else {
        $item = "";
    }
    return $item;
}
?>
